package models

import (
	mysql2 "gitee.com/xuxinzhi2206/corehub/database/mysql"
	"gorm.io/gorm"
)

type Goods struct {
	gorm.Model
	GoodsName   string  `json:"goods_name"  gorm:"type:varchar(20);comment:商品名称"`
	GoodsStats  string  `json:"goods_stats" gorm:"type:varchar(20);comment:商品规格"`
	GoodsUrl    string  `json:"goods_url"   gorm:"type:varchar(255);comment:商品图片"`
	GoodsShop   string  `json:"goods_shop"  gorm:"type:varchar(50);comment:所属店铺"`
	GoodsNum    int     `json:"goods_num"   gorm:"type:int(11);comment:购买数量"`
	GoodsPrice  float64 `json:"goods_price" gorm:"type:decimal(10, 2);comment:售价"`
	CouponPrice float64 `json:"coupon_price" gorm:"type:decimal(10, 2);comment:优惠价"`
	CountPrice  float64 `json:"count_price"  gorm:"type:decimal(10, 2);comment:实付价"`
	Preheating  int     `json:"preheating" gorm:"type:int(11);comment:预热,1预热"`
	GoodsStatus int     `json:"goods_status" gorm:"type:int(11);comment:商品状态,0已下架"`
	GoodsSort   int     `json:"goods_sort" gorm:"type:int(11);comment:商品分类"`
}

func (g *Goods) Add(add Goods) (info *Goods, err error) {
	return info, mysql2.WithMysql(func(db *gorm.DB) error {
		return db.Table("goods").Create(&add).Error
	})
}

func (g *Goods) Del(id int) (info *Goods, err error) {
	return info, mysql2.WithMysql(func(db *gorm.DB) error {
		return db.Table("goods").Where("id = ?", id).Delete(&info).Error
	})
}

func (g *Goods) List() (info []Goods, err error) {
	return info, mysql2.WithMysql(func(db *gorm.DB) error {
		return db.Table("goods").Find(&info).Error
	})
}

func (g *Goods) FindByStatus() (info *Goods, err error) {
	return info, mysql2.WithMysql(func(db *gorm.DB) error {
		return db.Table("goods").Where("goods_status = 1").First(&info).Error
	})
}

// 商品分类，价格区间，销量
func (g *Goods) FindByAny(orderType int, text string) (info []Goods, err error) {
	return info, mysql2.WithMysql(func(db *gorm.DB) error {
		switch orderType {
		case 1:
			db.Table("goods").Where("goods_sort = ?", text)
			break
		case 2:
			db.Table("goods").Where("goods_price > 30 ")
			break
		case 3:
			db.Table("goods").Where("goods_num = ?", text)
			break
		default:
			break
		}
		return db.Table("goods").Find(&info).Error
	})
}

// 优先展示预热商品，提高商品的曝光率和销售量
func (g *Goods) FindByPreheating() (info []Goods, err error) {
	return info, mysql2.WithMysql(func(db *gorm.DB) error {
		return db.Table("goods").Where("preheating = 1").Find(&info).Error
	})
}

func (g *Goods) FindByID(id int) (info *Goods, err error) {
	return info, mysql2.WithMysql(func(db *gorm.DB) error {
		return db.Table("goods").Where("id = ?", id).First(&info).Error
	})
}

func (g *Goods) FindBySort(sort int) (info []Goods, err error) {
	return info, mysql2.WithMysql(func(db *gorm.DB) error {
		return db.Table("goods").Where("goods_sort = ?", sort).Find(&info).Error
	})
}

// 修改管理库存
func (g *Goods) UpdateByNum(id, GoodsNum int) (info *Goods, err error) {
	return info, mysql2.WithMysql(func(db *gorm.DB) error {
		return db.Table("goods").Where("id = ?", id).UpdateColumn("goods_num", GoodsNum).Error
	})
}
